package at.fhj.itm;
// TODO Add JavaDoc Documentation Comments

/**
 * 
 * 
 * 
 * @author Alex
 * @version: 1.0
 * 
 * 
 *
 */
public class Queue {
	
/** Describes the maximum size of queArray*/
  private int maxSize;
 
/** Describes the maximum size of queArray */
  private long[] queArray;

/** Shows first set entry of queArray */
  private int front;

/** Shows last set entry of queArray */
  private int rear;
  
/** Shows actual number of set entries of queArray */
  
  private int nItems;

 /**
  * 
  
  * 
  * @param s sets value of maximum size 
  * @param maxSize 
  */
  public Queue(int s) {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
  }

  /** 
   * increments value into the next empty position in the array. 
   * If rear is one lower than maxSize, rear is set to -1
   * 
   * @param j 		passed value when method is called. 
   * @param rear 	if rear is one lower than maxSize it's set to -1. 
   * @param nItems	per call nItems set +1	
   * 				
   */
  public void insert(long j) {
    if (rear == maxSize - 1) // deal with wraparound
      rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; 
  }

  //   take item from front of queue
  /**
   * Deletes entry on first set position of Queue
   * 
   * @param temp 	temporary storage of the first set value in Queue
   * @return temp	value of first array position, which is meant to be removed
   */
  public long remove() {
    long temp = queArray[front++]; // get value and incr front
    if (front == maxSize) // deal with wraparound
      front = 0;
    nItems--; // one less item
    return temp;
  }
  
  /**
   * Returns the first set entry of the Queue
   * 
   * @return	value of the first set entry in Queue
   */

  public long peekFront() {
    return queArray[front];
  }
  
  /**
   * 
   * @return boolean true is returned if nItems is set 0
   */

  public boolean isEmpty() {
    return (nItems == 0);
  }

  /**
   * 
   * @return boolean true is returned if nItems is equal to maxSize
   */
  public boolean isFull() {
    return (nItems == maxSize);
  }

  /**
   * Shows actual size of Queue wit the value of nItems
   * 
   * @return value of nItems
   */
  public int size() {
    return nItems;
  }

  // TODO Refactor / Delete Main Method and create JUnit test code
  
  
  public static void main(String[] args) {
    Queue theQueue = new Queue(5); // queue holds 5 items

    theQueue.insert(10);
    theQueue.insert(20);
    theQueue.insert(30);
    theQueue.insert(40);

    theQueue.remove(); 
    theQueue.remove(); 
    theQueue.remove();

    theQueue.insert(50); 
    theQueue.insert(60); //    (wraps around)
    theQueue.insert(70);
    theQueue.insert(80);

    while (!theQueue.isEmpty()) {
      long n = theQueue.remove(); // (40, 50, 60, 70, 80)
      System.out.print(n);
      System.out.print(" ");
    }
    System.out.println("");
  }
}
           
         